package com.atlantis.apps.notifier.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.atlantis.apps.notifier.R;
import com.atlantis.apps.notifier.util.SNConstants;
import com.atlantis.apps.notifier.util.SNUtility;

public class SNCheckNowActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checknow);

		Button button = (Button) findViewById(R.id.checkNowBtn);
		button.setOnClickListener(checkNow);

		updateView();

	}

	private OnClickListener checkNow = new OnClickListener() {
		public void onClick(View v) {
			
			try {
				SNUtility.updateSongView(v.getContext());
				updateView();
				Toast.makeText(v.getContext(), R.string.songsupdated, Toast.LENGTH_SHORT).show();
			} catch (Exception e) {
				Toast.makeText(v.getContext(), R.string.settingserror, Toast.LENGTH_LONG).show();
				Log.d(v.getContext().getClass().getSimpleName(), "Check your Settings!");
			}
		}
	};

	private void updateView() {
		SharedPreferences mySharedPreferences = getSharedPreferences(SNConstants.SNSETTINGS, Activity.MODE_PRIVATE);

		if (mySharedPreferences != null) {
			TextView noOfSongs = (TextView) findViewById(R.id.numOfSongsDisplay);
			noOfSongs.setText(mySharedPreferences.getString(SNConstants.SNNOOFSONGS, ""));

			TextView lastUpdated = (TextView) findViewById(R.id.lastUpdateDisplay);
			lastUpdated.setText(mySharedPreferences.getString(SNConstants.SNLASTUPDATE, ""));

			Log.d(getClass().getSimpleName(), "Updated View");
		}

	}

	protected void onResume() {
		super.onResume();
		updateView();
	}
}